package com.shenxuchen.crm.web.controller;

import com.shenxuchen.crm.service.ValueService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("cache")
public class CacheController {

    @Resource
    private ValueService valueService;

    @GetMapping("options")
    public Object getOptions(String code, HttpSession session) {
        ServletContext servletContext = session.getServletContext();
        Map optionsMap = (Map) servletContext.getAttribute("optionsCache");
        Object options = optionsMap.get(code);
        if (options == null) {
            options = valueService.loadByCode(code);
            optionsMap.put(code, options);
        }
        return options;
    }

    @GetMapping("allOptions")
    public Object getAllOptions(HttpSession session) {
        ServletContext servletContext = session.getServletContext();
        return servletContext.getAttribute("optionsCache");
    }
}
